﻿using MindASPCoreWebAPI.DtoModel.SystemManager;
using MindASPCoreWebAPI.IRepository.SystemManager;
using MindASPCoreWebAPI.PoModel.SystemManager;
using MindASPCoreWebAPI.Repository.CommonLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MindASPCoreWebAPI.Repository.SystemManager
{
    //公司管理（DAL）
    public class CompanyAdminRepository : BaseRepository, ICompanyAdminRepository
    {
        // 上下文       
        public CompanyAdminRepository(SystemCenterMindContext systemcentermindContext) : base(systemcentermindContext)
        {
        }

        //行业下拉
        public List<IndustryAdminModel> Down_IndustryAdminModel()
        {
            return _systemCenterDbSoaContext.IndustryAdminModel.ToList();
        }

        //公司性质下拉
        public List<NatModel> Down_NatModel()
        {
            return _systemCenterDbSoaContext.NatModel.ToList();
        }

        //公司KA下拉
        public List<KaModel> Down_KaModel()
        {
            return _systemCenterDbSoaContext.KaModel.ToList();
        }


        //显示公司管理
        public List<CompanyAdminDto> Show_CompanyAdminModel(out int totalcount,int pageindex, int pagesize, int? com_ind_id, int? com_state, int? com_ka_id, int? com_mp4, int? com_ad, string com_name = "")
        {
            var sql = from a in _systemCenterDbSoaContext.CompanyAdminModel
                      join b in _systemCenterDbSoaContext.NatModel on a.nat_id equals b.nat_id
                      join c in _systemCenterDbSoaContext.KaModel on a.com_ka_id equals c.com_ka_id
                      join d in _systemCenterDbSoaContext.IndustryAdminModel on a.com_ind_id equals d.com_ind_id
                      select new CompanyAdminDto
                      {
                          com_id = a.com_id,
                          com_name = a.com_name,
                          com_ind_id = a.com_ind_id,
                          com_ind_name = d.com_ind_name,
                          com_state = a.com_state,
                          nat_id = a.nat_id,
                          nat_name = b.nat_name,
                          com_ka_id = a.com_ka_id,
                          com_ka_name = c.com_ka_name,
                          com_mp4 = a.com_mp4,
                          com_ad = a.com_ad,
                          com_site = a.com_site,
                          com_order = a.com_order
                      };

            if (com_ind_id != null)
            {
                sql = sql.Where(a => a.com_ind_id == com_ind_id);
            }
            if (com_state != null)
            {
                sql = sql.Where(a => a.com_state == com_state);
            }
            if (com_ka_id != null)
            {
                sql = sql.Where(a => a.com_ka_id == com_ka_id);
            }
            if (com_mp4 != null)
            {
                sql = sql.Where(a => a.com_mp4 == com_mp4);
            }
            if (com_ad != null)
            {
                sql = sql.Where(a => a.com_ad == com_ad);
            }
            if (!string.IsNullOrWhiteSpace(com_name))
            {
                sql = sql.Where(a => a.com_name.Contains(com_name));
            }


            totalcount = sql.Count();


            return sql.OrderBy(a => a.com_id).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();


        }

        //删除公司
       public int Delete_CompanyAdminModel(int com_id)
        {
            var sql = _systemCenterDbSoaContext.CompanyAdminModel.Find(com_id);
            _systemCenterDbSoaContext.CompanyAdminModel.Remove(sql);
            return _systemCenterDbSoaContext.SaveChanges();
        }


       //详情
      public CompanyAdminModel Eait_Delete_CompanyAdminModel(int com_id)
        {
            return _systemCenterDbSoaContext.CompanyAdminModel.FirstOrDefault(a => a.com_id == com_id);
        }


        //修改
        public int Update_CompanyAdminModel(CompanyAdminModel companyAdminModel)
        {
            _systemCenterDbSoaContext.Entry(companyAdminModel).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _systemCenterDbSoaContext.SaveChanges();
        }

        //添加
        public int Add_CompanyAdminModel(CompanyAdminModel companyAdminModel)
        {
            _systemCenterDbSoaContext.CompanyAdminModel.Add(companyAdminModel);
            return _systemCenterDbSoaContext.SaveChanges();
        }
    }
}
